package yur.wedding.modules.sys.service.impl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.transaction.annotation.Transactional;
import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysUserRoleDao;
import yur.wedding.modules.sys.entity.SysUserRoleEntity;
import yur.wedding.modules.sys.service.SysUserRoleService;
import yur.wedding.utils.PageUtils;


@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleDao, SysUserRoleEntity> implements SysUserRoleService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysUserRoleEntity> page = this.page(
                new Query<SysUserRoleEntity>().getPage(params),
                new QueryWrapper<SysUserRoleEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 用户角色对应的角色
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> queryRoleIdList(Long userId) {
        List<Long> roleId = baseMapper.queryRoleIdList(userId);
        return roleId;
    }

    /**
     * 添加用户与角色的关系
     *
     * @param userId
     * @param roleIdList
     */
    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        //执行修改操作时先删除用户与角色的关系
        this.remove(new QueryWrapper<SysUserRoleEntity>().eq("user_id", userId));

        if (roleIdList.size() == 0 || roleIdList == null) {
            return;
        }

        for (Long roleid : roleIdList) {
            SysUserRoleEntity userRoleEntity = new SysUserRoleEntity();
            userRoleEntity.setRoleId(roleid);
            userRoleEntity.setUserId(userId);
            this.save(userRoleEntity);
        }
    }


    /**
     * 查询角色下面的用户
     * @param role_id
     * @return
     */
    @Override
    public List<Long> roleuserlist(Long role_id) {

        List<Long> roleId = baseMapper.roleuserlist(role_id);

        return roleId;
    }

    /**
     * 批量删除角色下面的用户
     * @param useridList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeUserIdList(Long[] useridList,Long roleId) {
        Map<String, Object> map =new HashMap<>();
        map.put("roleId",roleId);
        map.put("useridList",useridList);

        baseMapper.removeUserIdList(map);
    }

    /**
     * 批量添加角色下面的用户
     * @param userIdroleIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeUserIdsave(String userIdroleIds) {
    System.out.println("用户ID" + userIdroleIds);
        JSONObject jsonObject = JSONObject.fromObject(userIdroleIds);
        JSONArray jsonArr = (JSONArray) jsonObject.get("userIDs");
        String roleId1 = (String) jsonObject.get("roleid");
        Long roleId=Long.valueOf(roleId1);

        Long useridList[] = new Long[jsonArr.size()];
        for (int i = 0; i < jsonArr.size(); i++) {
            useridList[i] = Long.valueOf(jsonArr.get(i).toString());
        }

        Map<String, Object> map =new HashMap<>();
        map.put("roleId",roleId);
        map.put("useridList",useridList);
        baseMapper.removeUserIdsave(map);
    }

}
